//
//  BindingPhoneController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/13.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "BindingPhoneController.h"
#import "PhoneFieldView.h"
#import "VerificationCodeView.h"
#import "BindingEmailController.h"

@interface BindingPhoneController ()
/**手机号输入框*/
@property (nonatomic,strong)PhoneFieldView*phoneView;
/**验证码输入框*/
@property (nonatomic,strong)VerificationCodeView*codeView;

/**协议点击文字*/
@property (nonatomic,strong)AgreementView*agreementView;

/**确定按钮*/
@property (nonatomic,strong)UIButton*loginBtn;

@end

@implementation BindingPhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :UIColorFromRGB(0x24B34A), NSFontAttributeName : SYSTEMFONT(19)}];
    self.title = @"绑定手机号";
    [self createUI];
    [self setNotificaAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndClearColor];
}

-(void)setNotificaAction{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chaneTextFiel) name:UITextFieldTextDidChangeNotification object:nil];
}
-(void)chaneTextFiel{
    if (self.phoneView.textFile.text.length > 0 && self.codeView.textFile.text.length >0 && self.agreementView.isSelected) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}
/**
 绑定已有邮箱账号
 */
-(void)bindingEmailAction{
    [self.navigationController pushViewController:[BindingEmailController new] animated:YES];
}
-(void)createUI{
    UIImageView * topImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"login_icon")];
    
    UILabel * topTitle = UILabel.new;
    topTitle.text = @"用户名";
    topTitle.textColor = UIColorFromRGB(0xC2CEC5);
    topTitle.font = SYSTEMFONT(16);
    topTitle.textAlignment = NSTextAlignmentCenter;
    
    UILabel * bottomTex = UILabel.new;
    bottomTex.text = @"绑定已有邮箱账号";
    bottomTex.font =SYSTEMFONT(15);
    bottomTex.textColor = UIColorFromRGB(0x24B34A);
    bottomTex.textAlignment = NSTextAlignmentCenter;
    
    bottomTex.userInteractionEnabled = YES;
    [bottomTex addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bindingEmailAction)]];
    
    [self.view addSubview:topImage];
    [self.view addSubview:topTitle];
    [self.view addSubview:self.phoneView];
    [self.view addSubview:self.codeView];
    
    [self.view addSubview:self.agreementView];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:bottomTex];
    
    
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(88);
        make.width.height.mas_equalTo(80);
    }];
    
    [topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(topImage.mas_bottom).offset(10);
    }];
    
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(topImage.mas_bottom).offset(45);
        make.height.mas_equalTo(40);
    }];
    
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.phoneView.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
    }];
    
    [self.agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(55);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(self.codeView.mas_bottom).offset(60);
    }];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.agreementView.mas_bottom).offset(15);
        make.height.mas_equalTo(40);
    }];
    
    [bottomTex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.loginBtn.mas_bottom).offset(15);
    }];
    
    
}

#pragma mark ---懒加载
-(PhoneFieldView*)phoneView{
    if(!_phoneView){
        _phoneView=[[PhoneFieldView alloc] initWithFrame:CGRectZero];
    }
    return _phoneView;
}
-(VerificationCodeView*)codeView{
    if(!_codeView){
        _codeView=[[VerificationCodeView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        _codeView.downBlcok = ^{
            [weakSelf getDownAction:weakSelf.codeView.countdown];
        };
    }
    return _codeView;
}
-(AgreementView*)agreementView{
    if(!_agreementView){
        _agreementView=[[AgreementView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        _agreementView.touchHandler = ^{
            [weakSelf chaneTextFiel];
        };
    }
    return _agreementView;
}

-(UIButton*)loginBtn{
    if(!_loginBtn){
        _loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_loginBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x9ADCAB)] forState:UIControlStateDisabled];
        [_loginBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x24B34A)] forState:UIControlStateNormal];
        _loginBtn.enabled = NO;
        
    }
    return _loginBtn;
}
@end
